package com.coin.service;

import com.coin.common.AccountType;
import com.coin.common.MemberAccountStatus;
import com.coin.common.MemberType;
import com.coin.dao.account.AccountMapper;
import com.coin.dao.account.MemberMapper;
import com.coin.dao.model.Account;
import com.coin.dao.model.Member;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * Created by chenhao on 2017/5/18.
 */
@Service
@Transactional
public class MemberService {
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private AccountMapper accountMapper;

    public boolean addMemberInfo(Member member) {
        memberMapper.insertMainTable(member);
        member.setTableId(String.valueOf(member.getId() % 16));
        return 1 == memberMapper.insertSelective(member);
    }

    public boolean addMemberInfo(String name, String wxNumber, String zfbNumber, String phoneNumber) {
        Member member = new Member();
        member.setCreateTime(new Date());
        member.setMemberName(name);
        member.setWxNumber(wxNumber);
        member.setWxStatus(MemberAccountStatus.init);
        member.setZfbNumber(zfbNumber);
        member.setZfbStatus(MemberAccountStatus.init);
        member.setPhone(phoneNumber);

        boolean j = 1 == memberMapper.insertMainTable(member);
        member.setTableId(String.valueOf(member.getId() % 16));
        boolean s = 1 == memberMapper.insertSelective(member);

        //插入账户信息
        Account account = new Account();
        account.setMemberId(member.getId());
        account.setMemberType(MemberType.member);
        account.setAccountType(AccountType.memberBalance);
        account.setCreateTime(new Date());
        account.setUpdateTime(new Date());
        account.setBalance(BigDecimal.ZERO);
        accountMapper.insertMainTable(account);

        account.setTableId(String.valueOf(member.getId() % 16));
        boolean t = 1 == accountMapper.insert(account);

        return j && s && t;
    }
}
